var path = require('path');
var config = require('../../../build/configuration/config');

module.exports.register = function(Handlebars) {
  Handlebars.registerHelper('layout', function(options) {
    var file;
    var relative;
    var currentPath;
    var filePath;

    if (options.data.file) {
      file = options.data.file;
    } else {
      file = options.data.root.file;
    }

    if (path.sep !== '/') {
      filePath = path
        .relative(file.cwd, path.dirname(file.path))
        .split(path.sep)
        .join('/');
    } else {
      filePath = path.relative(file.cwd, path.dirname(file.path));
    }

    relative = path.posix.relative(config.html.pages, filePath);
    currentPath = path.posix.join(config.html.build, relative);

    return new Handlebars.SafeString(path.posix.relative(currentPath, config.layout.build));
  });
};
